package com.alibaba.cloud.encry;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/**
 * A factory for creating SecrteKey objects.
 *
 * @ClassName: CreateSecrteKey
 * @Description:创建公钥私钥 
 * @author: zhb zhb1316783262@163.com
 * @date: 2018年3月14日 下午7:29:10 引进的包都是Java自带的jar包 秘钥相关包 base64 编解码 这里只用到了编码
 */
public class SecrteKeyFactory {
    
    /** 公钥. */
    private static RSAPublicKey rsaPublicKey;
    
    /** 私钥. */
    private static RSAPrivateKey rsaPrivateKey;
    
    /** The Constant KEY_ALGORITHM. */
    public static final String KEY_ALGORITHM = "RSA";
    
    /** The Constant SIGNATURE_ALGORITHM. */
    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
    
    /** AES. */
    private static SecretKey secretKey;
    /**
     * @Description: 初始化生成公钥和私钥对
     * @author: zhb zhb1316783262@163.com
     * @date: 2018年3月13日 下午3:13:22
     */
    static {
        try {
            // 初始化密钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
            keyPairGenerator.initialize(512);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            // 生成公钥
            rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
            // 生成私钥
            rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);// size
            SecretKey generateKey = keyGenerator.generateKey();
            byte[] keyBytes = generateKey.getEncoded();
            secretKey = new SecretKeySpec(keyBytes, "AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the public key.
     *
     * @return the public key
     * @throws Exception the exception
     */
    // 获得公钥
    public static String getPublicKey() throws Exception {
        return encryptBASE64(rsaPublicKey.getEncoded());
    }

    /**
     * Gets the private key.
     *
     * @return the private key
     * @throws Exception the exception
     */
    // 获得私钥
    public static String getPrivateKey() throws Exception {
        // 编码返回字符串
        return encryptBASE64(rsaPrivateKey.getEncoded());
    }

    /**
     * Encrypt BASE 64.
     *
     * @param key the key
     * @return the string
     * @throws Exception the exception
     */
    // 编码返回字符串
    public static String encryptBASE64(byte[] key) throws Exception {
        return new String(Base64.getEncoder().encode(key));
    }

    /**
     * Decrypt BASE 64.
     *
     * @param key the key
     * @return the byte[]
     * @throws Exception the exception
     */
    // 解码返回 byte
    public static byte[] decryptBASE64(String key) throws Exception {
        return Base64.getDecoder().decode(key);
    }

    /**
     * Generate aes key.
     *
     * @return the string
     * @throws Exception the exception
     */
    // 获得公钥
    public static String generateAesKey() throws Exception {
        return encryptBASE64(secretKey.getEncoded());
    }
}